#include "mypushbutton.h"

#include<QDebug>
#include<QPropertyAnimation>
//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{

//}

//重写MyPushButton构造函数
MyPushButton::MyPushButton(QString normalImg,QString pressImg){
        this->normalImgPpath=normalImg;
    this->pressImgPath=pressImg;

    //加载back图片并设置大小
    QPixmap  pix;
    bool ret=pix.load(normalImg);
    if(!ret){
        qDebug()<<"false";
    }

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0pxl}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

};


void MyPushButton::zoom1(){
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    //设置动画的时间间隔
    animation->setDuration(200);

    //设置按钮位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();

}
void MyPushButton::zoom2(){
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    //设置动画的时间间隔
    animation->setDuration(200);

    //设置按钮位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();

}


void MyPushButton::mousePressEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){//传入的按下图片不为空，说明明需要按下切换图片
        QPixmap  pix;
        bool ret=pix.load(this->pressImgPath);
        if(!ret){
            qDebug()<<"false";
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0pxl}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

    }
    //让父类执行其他的内容
    return QPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){//传入的按下图片不为空，说明明需要按下切换图片
        QPixmap  pix;
        bool ret=pix.load(this->normalImgPpath);
        if(!ret){
            qDebug()<<"false";
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0pxl}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

    }
    return QPushButton::mouseReleaseEvent(e);
}




